﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Net;

namespace KoSoft
{
	public class UtilEml
	{
		private string host;
		private int port;
		private string username;
		private string password;
		private string emlfrom;
		private string emlto;
		private string subject;
		private string body;
		private string attached;

		public UtilEml()
		{

		}

		public bool SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
		{
			try
			{
				//生成一个 使用SMTP发送邮件的客户端对象
				SmtpClient client = new SmtpClient(strSmtpServer);
				//表示以当前登录用户的默认凭据进行身份验证
				client.UseDefaultCredentials = true;
				//包含用户名和密码
				client.Credentials = new NetworkCredential(UserName, Password);
				//指定如何发送电子邮件。
				//Network 电子邮件通过网络发送到 SMTP 服务器。 
				//PickupDirectoryFromIis       将电子邮件复制到挑选目录，然后通过本地 Internet 信息服务 (IIS) 传送。  
				//SpecifiedPickupDirectory     将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录，然后由外部应用程序传送。  
				client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
				//建立邮件对象 
				MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
				//定义邮件正文，主题的编码方式
				message.BodyEncoding = Encoding.GetEncoding("UTF-8");
				message.SubjectEncoding = Encoding.GetEncoding("UTF-8");
				//获取或设置一个值，该值指示电子邮件正文是否为 HTML。 
				message.IsBodyHtml = true;
				//指定邮件优先级
				message.Priority = MailPriority.Normal;
				//添加附件
				//System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@"f:/baihe.txt"); 
				if (strFileName != "" && strFileName != null)
				{
					Attachment data = new Attachment(strFileName);
					message.Attachments.Add(data);
				}
				client.Credentials = new NetworkCredential(strFrom, Password);
				client.Send(message);
			}
			catch (SmtpException ex)
			{
				throw ex;
			}
			return true;
		}
	}
}